package com.idbk.component.ui.image;

import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher.OnPhotoTapListener;
import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;




import com.idbk.uicomponent.R;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

/**
 * 图片浏览器
 * @author lupc
 *
 */
public class MyImageViewer extends LinearLayout {
	
	private Context mContext;

	/* 显示容器 */
	private HackyViewPager mViewPager;
	private MyAdapter mAdapter;

	/**
	 * 指示器 容器
	 */
	private LinearLayout mTipRoot;

	/**
	 * 上个位置
	 */
	private int mPreviousPointPosition = 0;
	
	private String[] mURLs;
	
	private final static String DEFAULT_URL = "default";

	public MyImageViewer(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		
		LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.view_image_scanner, this);	

		mTipRoot = (LinearLayout)findViewById(R.id.mybanner_tipgroup);

		mURLs = new String[1];
		mURLs[0] = DEFAULT_URL;
		
		mViewPager = (HackyViewPager)findViewById(R.id.mybanner_image);
		mAdapter = new MyAdapter();
		mViewPager.setAdapter(mAdapter);
		MyPagerListener mMyPagerListener = new MyPagerListener();
		mViewPager.addOnPageChangeListener(mMyPagerListener);
	}

	public void initImages(List<String> URLs){
		mURLs = new String[URLs.size()]; 
		URLs.toArray(mURLs);
		
		//初始化 指示器
		mTipRoot.removeAllViews();
		for (int i = 0; i < URLs.size(); i++) {
			//添加
			mTipRoot.addView(getDot());
		}		
		showIndicator(0);
		
		mAdapter.notifyDataSetChanged();
	}

	private ImageView getDot(){
		ImageView dot = new ImageView(mContext);
		dot.setImageResource(R.drawable.dot_normal);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				new ViewGroup.LayoutParams(
				LayoutParams.WRAP_CONTENT,      
				LayoutParams.WRAP_CONTENT));		
		params.rightMargin = 3;  
		params.leftMargin = 3;
		dot.setLayoutParams(params);
		return dot;		
	}
	
	private void showIndicator(int position){
		//设定指示器状态
		((ImageView)(mTipRoot.getChildAt(mPreviousPointPosition)))
			.setImageResource(R.drawable.dot_normal);
		((ImageView)(mTipRoot.getChildAt(position)))
			.setImageResource(R.drawable.dot_current);
		//记录位置
		mPreviousPointPosition = position;
	}
	
	/**
	 * viewpager数据适配器
	 * @author lupc
	 *
	 */
	private class MyAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return mURLs.length;
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object ;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			PhotoView photoView = new PhotoView(container.getContext());
			photoView.setScaleType(ScaleType.CENTER_INSIDE);
			photoView.setOnPhotoTapListener(new OnPhotoTapListener() {
				@Override
				public void onPhotoTap(View view, float x, float y) {
					if(mContext instanceof Activity){
						((Activity)mContext).finish();
					}
				}
				@Override
				public void onOutsidePhotoTap() {
					if(mContext instanceof Activity){
						((Activity)mContext).finish();
					}
				}				
			});
			
			Picasso.with(mContext)			
            .load(mURLs[position])
            .memoryPolicy(MemoryPolicy.NO_CACHE)
            .placeholder(R.drawable.ico_preimg_loading)
            .error(R.drawable.ico_preimg_net_error)
            .into(photoView);

			container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			return photoView;
		}

	}


	private class MyPagerListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int arg0) {
			showIndicator(arg0);
		}

	}
}
